//    Copyright (C) 2013  <lugocreation@gmail.com>
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>.
package org.jpgolay.me.order;

import java.awt.Color;
import java.awt.Component;
import java.awt.event.*;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.math.BigDecimal;
import java.util.*;
import javax.swing.*;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.*;
import org.jpgolay.gui.fixed.*;
import org.jpgolay.me.agency.Agency;
import org.jpgolay.me.batch.Batch;
import org.jpgolay.me.buy.BuysPiece;
import org.jpgolay.me.customer.Customer;
import org.jpgolay.me.discount.DiscountRate;
import org.jpgolay.me.item.Item;
import org.jpgolay.me.payment.Payment;
import org.jpgolay.me.persistence.ObjectCRUD;
import org.jpgolay.me.sale.SalesBillPiece;
import org.jpgolay.me.sale.SalesPiece;
import org.jpgolay.me.salesperson.SalesPerson;
import org.jpgolay.me.stock.log.LogStock;
import org.jpgolay.me.supplier.Supplier;
import org.jpgolay.me.tax.Tax;
import org.jpgolay.me.util.MEDate;
import org.jpgolay.me.util.Parameter;

// Referenced classes of package org.jpgolay.me.order:
//            OrderGUI, PopupOrderStockRowsGUI, OrderRow, ModifyOrderController, 
//            ShippingOrderController
public class OrderStockController extends OrderGUI {

    public class LabelCellRenderer extends DefaultTableCellRenderer {

        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int col) {
            Component comp = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, col);
            label = (JLabel) comp;
            label.setForeground(Color.WHITE);
            return label;
        }
        protected JLabel label;

        public LabelCellRenderer() {
        }
    }

    private class MouseClickAgency extends MouseAdapter {

        public void mouseClicked(MouseEvent e) {
            if (e.getClickCount() == 2 && editable) {
                initModifyOrder();
            }
        }

        MouseClickAgency() {
        }
    }

    private class MouseClickShipping extends MouseAdapter {

        public void mouseClicked(MouseEvent e) {
            if (e.getClickCount() == 2 && editable) {
                initShippingOrder();
            }
        }

        MouseClickShipping() {
        }
    }

    public OrderStockController(Object piece, int pieceType, Parameter param) {
        taxJComboBox = null;
        calculateRow = false;
        editable = false;
        lock = true;
        updateRowsTable = true;
        write = false;
        this.pieceType = pieceType;
        this.param = param;
        desktop = param.getDesktop();
        crud = new ObjectCRUD();
        labelRenderer = new LabelCellRenderer();
        initLaw();
        initObjectPiece(piece);
        initDependences();
        initAll();
        initTaxValues();
        initRowsValues();
        initTotalValues();
    }

    public OrderStockController(Object piece, Agency agency, SalesPerson salesPerson, int pieceType, Parameter param) {
        taxJComboBox = null;
        calculateRow = false;
        editable = false;
        lock = true;
        updateRowsTable = true;
        write = false;
        this.agency = agency;
        this.salesPerson = salesPerson;
        this.pieceType = pieceType;
        this.param = param;
        desktop = param.getDesktop();
        crud = new ObjectCRUD();
        labelRenderer = new LabelCellRenderer();
        rowsList = new ArrayList();
        initLaw();
        editable = true;
        initObjectPiece(piece);
        initObjectPieceValues(piece);
        initAll();
    }

    private void initLaw() {
        if (param.getAccessLaw().length() >= 27 && param.getAccessLaw().charAt(27) == '1') {
            write = true;
        }
    }

    private void initAll() {
        green = new ImageIcon(getClass().getResource("/org/jpgolay/me/images/green12x12.png"));
        orange = new ImageIcon(getClass().getResource("/org/jpgolay/me/images/orange12x12.png"));
        red = new ImageIcon(getClass().getResource("/org/jpgolay/me/images/red12x12.png"));
        initInfoField(" ");
        initOrderListener();
        initTaxTable();
        initAgencyPanel();
        initAgencyValues();
        if (pieceType == 5 || pieceType == 6) {
            initSupplierPanel();
        } else {
            initCustomerPanel();
        }
        initReferencesValues();
        initReferencesPanel();
        initRowsTable();
        initDueDateField();
        discount = new DiscountRate(param);
        initTotalPanel();
    }

    private void initOrderListener() {
        getPopupOrderStockRows().getCancelPreparation().addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                actionCancelPreparation();
            }
        });
        getPopupOrderStockRows().getDeleteLine().addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                actionDeleteLine();
            }
        });
    }

    private void initObjectPiece(Object piece) {
        if (piece instanceof SalesPiece) {
            salesPiece = (SalesPiece) piece;
            pieceId = salesPiece.getId();
            pieceDate = salesPiece.getDate();
        } else if (piece instanceof SalesBillPiece) {
            salesBillPiece = (SalesBillPiece) piece;
            pieceId = salesBillPiece.getId();
            pieceDate = salesBillPiece.getDate();
        } else if (piece instanceof BuysPiece) {
            buysPiece = (BuysPiece) piece;
            pieceId = buysPiece.getId();
            pieceDate = buysPiece.getDate();
        }
        if (pieceDate.equals("")) {
            pieceDate = (new MEDate()).getPlain();
        }
    }

    private void initObjectPieceValues(Object piece) {
        if (piece instanceof SalesPiece) {
            salesPiece.setGrossPrice(new BigDecimal("0.00"));
            salesPiece.setNetPrice(new BigDecimal("0.00"));
            salesPiece.setShippingPrice(new BigDecimal("0.00"));
            salesPiece.setShippingTax(new BigDecimal("0.00"));
            salesPiece.setPartPayment(new BigDecimal("0.00"));
            salesPiece.setTotalTax(new BigDecimal("0.00"));
            salesPiece.setTax1(new BigDecimal("0.00"));
            salesPiece.setTax2(new BigDecimal("0.00"));
            salesPiece.setTax3(new BigDecimal("0.00"));
            salesPiece.setTax4(new BigDecimal("0.00"));
            salesPiece.setTax5(new BigDecimal("0.00"));
            salesPiece.setSendingId(new BigDecimal("0.00"));
            salesPiece.setPrepare(new Integer(0));
        } else if (piece instanceof SalesBillPiece) {
            salesBillPiece.setGrossPrice(new BigDecimal("0.00"));
            salesBillPiece.setNetPrice(new BigDecimal("0.00"));
            salesBillPiece.setShippingPrice(new BigDecimal("0.00"));
            salesBillPiece.setShippingTax(new BigDecimal("0.00"));
            salesBillPiece.setPartPayment(new BigDecimal("0.00"));
            salesBillPiece.setTotalTax(new BigDecimal("0.00"));
            salesBillPiece.setTax1(new BigDecimal("0.00"));
            salesBillPiece.setTax2(new BigDecimal("0.00"));
            salesBillPiece.setTax3(new BigDecimal("0.00"));
            salesBillPiece.setTax4(new BigDecimal("0.00"));
            salesBillPiece.setTax5(new BigDecimal("0.00"));
            salesBillPiece.setSubTotal(new BigDecimal("0.00"));
            salesBillPiece.setLocking(new Integer(0));
        } else if (piece instanceof BuysPiece) {
            buysPiece.setGrossPrice(new BigDecimal("0.00"));
            buysPiece.setNetPrice(new BigDecimal("0.00"));
            buysPiece.setShippingPrice(new BigDecimal("0.00"));
            buysPiece.setShippingTax(new BigDecimal("0.00"));
            buysPiece.setPartPayment(new BigDecimal("0.00"));
            buysPiece.setTotalTax(new BigDecimal("0.00"));
            buysPiece.setTax1(new BigDecimal("0.00"));
            buysPiece.setTax2(new BigDecimal("0.00"));
            buysPiece.setTax3(new BigDecimal("0.00"));
            buysPiece.setTax4(new BigDecimal("0.00"));
            buysPiece.setTax5(new BigDecimal("0.00"));
            buysPiece.setLocking(new Integer(0));
        }
    }

    private void initDependences() {
        java.util.Collection list = null;
        switch (pieceType) {
            case 0: // '\0'
                agency = (Agency) crud.read(salesPiece.getCompagnyId(), 21);
                customer = (Customer) crud.read(salesPiece.getCustomerId(), 27);
                salesPerson = (SalesPerson) crud.read(salesPiece.getSalesPersonId(), 15);
                list = crud.readAll(salesPiece.getId(), 28);
                break;

            case 1: // '\001'
                agency = (Agency) crud.read(salesPiece.getCompagnyId(), 21);
                customer = (Customer) crud.read(salesPiece.getCustomerId(), 27);
                salesPerson = (SalesPerson) crud.read(salesPiece.getSalesPersonId(), 15);
                list = crud.readAll(salesPiece.getId(), 28);
                break;

            case 2: // '\002'
                agency = (Agency) crud.read(salesPiece.getCompagnyId(), 21);
                customer = (Customer) crud.read(salesPiece.getCustomerId(), 27);
                salesPerson = (SalesPerson) crud.read(salesPiece.getSalesPersonId(), 15);
                list = crud.readAll(salesPiece.getId(), 28);
                break;

            case 3: // '\003'
                agency = (Agency) crud.read(salesBillPiece.getCompagnyId(), 21);
                customer = (Customer) crud.read(salesBillPiece.getCustomerId(), 27);
                salesPerson = (SalesPerson) crud.read(salesBillPiece.getSalesPersonId(), 15);
                list = crud.readAll(salesBillPiece.getId(), 29);
                break;

            case 4: // '\004'
                agency = (Agency) crud.read(salesBillPiece.getCompagnyId(), 21);
                customer = (Customer) crud.read(salesBillPiece.getCustomerId(), 27);
                salesPerson = (SalesPerson) crud.read(salesBillPiece.getSalesPersonId(), 15);
                list = crud.readAll(salesBillPiece.getId(), 29);
                break;

            case 5: // '\005'
                agency = (Agency) crud.read(buysPiece.getCompagnyId(), 21);
                supplier = (Supplier) crud.read(buysPiece.getSupplierId(), 9);
                salesPerson = (SalesPerson) crud.read(buysPiece.getSalesPersonId(), 15);
                list = crud.readAll(buysPiece.getId(), 30);
                break;

            case 6: // '\006'
                agency = (Agency) crud.read(buysPiece.getCompagnyId(), 21);
                supplier = (Supplier) crud.read(buysPiece.getSupplierId(), 9);
                salesPerson = (SalesPerson) crud.read(buysPiece.getSalesPersonId(), 15);
                list = crud.readAll(buysPiece.getId(), 30);
                break;
        }
        if (list != null) {
            rowsList = new ArrayList(list);
        } else {
            rowsList = null;
        }
    }

    private void initAgencyPanel() {
        getAgencyPanel().addMouseListener(new MouseClickAgency());
    }

    private void initAgencyValues() {
        if (agency != null) {
            getAgencyPanel().setAddrLn1Label(agency.getAdresseUn());
            getAgencyPanel().setAddrLn2Label(agency.getAdresseDeux());
            getAgencyPanel().setCityLabel(agency.getVille());
            getAgencyPanel().setCompagnyLabel(agency.getRaisonSociale());
            getAgencyPanel().setStatusLabel(agency.getCivilite());
            getAgencyPanel().setZipLabel(agency.getCp());
            if (salesPiece != null) {
                salesPiece.setCompagnyId(agency.getId());
            }
            if (salesBillPiece != null) {
                salesBillPiece.setCompagnyId(agency.getId());
            }
            if (buysPiece != null) {
                buysPiece.setCompagnyId(agency.getId());
            }
        } else {
            getAgencyPanel().setAddrLn1Label("");
            getAgencyPanel().setAddrLn2Label("");
            getAgencyPanel().setCityLabel("");
            getAgencyPanel().setCompagnyLabel("");
            getAgencyPanel().setStatusLabel("");
            getAgencyPanel().setZipLabel("");
        }
    }

    private void initCustomerPanel() {
        if (customer != null) {
            getAddresseePanel().setIdValue(customer.getId());
            getAddresseePanel().setCompagnyLabel(customer.getRaisonSociale());
            getAddresseePanel().setStatusLabel(customer.getCivilite());
            if (pieceType == 2 && !customer.getAdresseLivrUn().equals("")) {
                getAddresseePanel().setAddrLn1Label(customer.getAdresseLivrUn());
                getAddresseePanel().setAddrLn2Label(customer.getAdresseLivrDeux());
                getAddresseePanel().setCityLabel(customer.getVilleLivr());
                getAddresseePanel().setZipLabel(customer.getCpLivr());
            } else {
                getAddresseePanel().setAddrLn1Label(customer.getAdresseFactUn());
                getAddresseePanel().setAddrLn2Label(customer.getAdresseFactDeux());
                getAddresseePanel().setCityLabel(customer.getVilleFact());
                getAddresseePanel().setZipLabel(customer.getCpFact());
            }
            getAddresseePanel().setNotesLabel(customer.getCommentaire());
            if (salesPiece != null) {
                salesPiece.setCustomerId(customer.getId());
            }
            if (salesBillPiece != null) {
                salesBillPiece.setCustomerId(customer.getId());
                salesBillPiece.setDueDate((new MEDate()).getDateAddXDay(salesBillPiece.getDate(), customer.getEcheancePaiement().intValue()));
            }
        } else {
            getAddresseePanel().setIdValue("");
            getAddresseePanel().setCompagnyLabel("");
            getAddresseePanel().setStatusLabel("");
            getAddresseePanel().setAddrLn1Label("(s\351l\351ctionner un client)");
            getAddresseePanel().setAddrLn2Label("");
            getAddresseePanel().setCityLabel("");
            getAddresseePanel().setZipLabel("");
            getAddresseePanel().setNotesLabel("");
        }
    }

    private void initSupplierPanel() {
        if (supplier != null) {
            getAddresseePanel().setIdValue(supplier.getId());
            getAddresseePanel().setCompagnyLabel(supplier.getRaisonSociale());
            getAddresseePanel().setStatusLabel(supplier.getCivilite());
            getAddresseePanel().setAddrLn1Label(supplier.getAdresseComUn());
            getAddresseePanel().setAddrLn2Label(supplier.getAdresseComDeux());
            getAddresseePanel().setCityLabel(supplier.getVilleCom());
            getAddresseePanel().setZipLabel(supplier.getCpCom());
            getAddresseePanel().setNotesLabel(supplier.getCommentaire());
            if (buysPiece != null) {
                buysPiece.setSupplierId(supplier.getId());
            }
        } else {
            getAddresseePanel().setIdValue("");
            getAddresseePanel().setCompagnyLabel("");
            getAddresseePanel().setStatusLabel("");
            getAddresseePanel().setAddrLn1Label("(s\351l\351ctionner un fournisseur)");
            getAddresseePanel().setAddrLn2Label("");
            getAddresseePanel().setCityLabel("");
            getAddresseePanel().setZipLabel("");
            getAddresseePanel().setNotesLabel("");
        }
    }

    private void initReferencesPanel() {
        getReferencesPanel().addPropertyChangeListener("date", new PropertyChangeListener() {
            public void propertyChange(PropertyChangeEvent e) {
                pieceDate = getReferencesPanel().getDate();
                if (salesPiece != null) {
                    salesPiece.setDate(pieceDate);
                } else if (salesBillPiece != null) {
                    salesBillPiece.setDate(pieceDate);
                } else if (buysPiece != null) {
                    buysPiece.setDate(pieceDate);
                }
                actionUpdatePiece();
            }
        });
        getReferencesPanel().addPropertyChangeListener("selectedIndexPiece", new PropertyChangeListener() {
            public void propertyChange(PropertyChangeEvent e) {
                int oldPieceType = pieceType;
                pieceType = getReferencesPanel().getSelectedIndexPiece();
                String prefix;
                switch (pieceType) {
                    case 0: // '\0'
                        prefix = "D";
                        break;

                    case 1: // '\001'
                        prefix = "C";
                        break;

                    case 2: // '\002'
                        prefix = "BL";
                        break;

                    case 3: // '\003'
                        prefix = "F";
                        break;

                    case 4: // '\004'
                        prefix = "A";
                        break;

                    case 5: // '\005'
                        prefix = "C";
                        break;

                    case 6: // '\006'
                        prefix = "F";
                        break;

                    default:
                        prefix = "";
                        break;
                }
                if (salesPiece != null) {
                    if (pieceType < 3) {
                        if (salesPiece.getPrepare().intValue() == 0) {
                            salesPiece.setPrefix(prefix);
                            actionUpdatePiece();
                            actionUpdatePayment(oldPieceType);
                        } else {
                            pieceType = oldPieceType;
                            getReferencesPanel().setSelectedIndexPiece(pieceType);
                        }
                    } else {
                        pieceType = oldPieceType;
                        getReferencesPanel().setSelectedIndexPiece(pieceType);
                    }
                } else if (salesBillPiece != null) {
                    if (pieceType > 2 && pieceType < 5) {
                        salesBillPiece.setPrefix(prefix);
                        actionUpdatePiece();
                        actionUpdatePayment(oldPieceType);
                    } else {
                        pieceType = oldPieceType;
                        getReferencesPanel().setSelectedIndexPiece(pieceType);
                    }
                } else if (buysPiece != null) {
                    if (pieceType > 4) {
                        buysPiece.setPrefix(prefix);
                        actionUpdatePiece();
                        actionUpdatePayment(oldPieceType);
                    } else {
                        pieceType = oldPieceType;
                        getReferencesPanel().setSelectedIndexPiece(pieceType);
                    }
                }
            }
        });
    }

    private void initReferencesValues() {
        getReferencesPanel().setDate(pieceDate);
        getReferencesPanel().setSelectedIndexPiece(pieceType);
        setReferencesPanelEnabled(editable);
        if (salesPerson != null) {
            getReferencesPanel().setSalespersonValue(salesPerson.getNom() + "  " + salesPerson.getPrenom());
        } else {
            getReferencesPanel().setSalespersonValue("");
        }
        getReferencesPanel().setIdValue(pieceId);
        if (salesPiece != null) {
            salesPiece.setDate(pieceDate);
            salesPiece.setSalesPersonId(salesPerson.getId());
            salesPiece.setUpdateUserId(param.getUserId());
        }
        if (salesBillPiece != null) {
            salesBillPiece.setDate(pieceDate);
            salesBillPiece.setSalesPersonId(salesPerson.getId());
            salesBillPiece.setUpdateUserId(param.getUserId());
        }
        if (buysPiece != null) {
            buysPiece.setDate(pieceDate);
            buysPiece.setSalesPersonId(salesPerson.getId());
            buysPiece.setUpdateUserId(param.getUserId());
        }
    }

    private void initTaxTable() {
        java.util.Collection list = crud.readAll(null, 18);
        if (list != null) {
            Tax tax = null;
            ArrayList taxList = new ArrayList(list);
            taxData = new Object[taxList.size()][3];
            Iterator it = taxList.iterator();
            for (int i = 0; it.hasNext(); i++) {
                tax = (Tax) it.next();
                taxData[i][0] = tax.getId();
                taxData[i][1] = tax.getTaux().setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue());
                taxData[i][2] = new BigDecimal("0.00");
            }

        } else {
            taxData = new Object[5][3];
            for (int i = 0; i < 5; i++) {
                taxData[i][0] = null;
                taxData[i][1] = null;
                taxData[i][2] = null;
            }

        }
        getTaxTable().setData(taxData);
    }

    private void initTaxValues() {
        switch (pieceType) {
            case 0: // '\0'
                taxData[0][2] = salesPiece.getTax1().setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue());
                taxData[1][2] = salesPiece.getTax2().setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue());
                taxData[2][2] = salesPiece.getTax3().setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue());
                taxData[3][2] = salesPiece.getTax4().setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue());
                taxData[4][2] = salesPiece.getTax5().setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue());
                break;

            case 1: // '\001'
                taxData[0][2] = salesPiece.getTax1().setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue());
                taxData[1][2] = salesPiece.getTax2().setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue());
                taxData[2][2] = salesPiece.getTax3().setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue());
                taxData[3][2] = salesPiece.getTax4().setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue());
                taxData[4][2] = salesPiece.getTax5().setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue());
                break;

            case 2: // '\002'
                taxData[0][2] = salesPiece.getTax1().setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue());
                taxData[1][2] = salesPiece.getTax2().setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue());
                taxData[2][2] = salesPiece.getTax3().setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue());
                taxData[3][2] = salesPiece.getTax4().setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue());
                taxData[4][2] = salesPiece.getTax5().setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue());
                break;

            case 3: // '\003'
                taxData[0][2] = salesBillPiece.getTax1().setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue());
                taxData[1][2] = salesBillPiece.getTax2().setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue());
                taxData[2][2] = salesBillPiece.getTax3().setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue());
                taxData[3][2] = salesBillPiece.getTax4().setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue());
                taxData[4][2] = salesBillPiece.getTax5().setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue());
                break;

            case 4: // '\004'
                taxData[0][2] = salesBillPiece.getTax1().setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue());
                taxData[1][2] = salesBillPiece.getTax2().setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue());
                taxData[2][2] = salesBillPiece.getTax3().setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue());
                taxData[3][2] = salesBillPiece.getTax4().setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue());
                taxData[4][2] = salesBillPiece.getTax5().setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue());
                break;

            case 5: // '\005'
                taxData[0][2] = buysPiece.getTax1().setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue());
                taxData[1][2] = buysPiece.getTax2().setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue());
                taxData[2][2] = buysPiece.getTax3().setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue());
                taxData[3][2] = buysPiece.getTax4().setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue());
                taxData[4][2] = buysPiece.getTax5().setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue());
                break;

            case 6: // '\006'
                taxData[0][2] = buysPiece.getTax1().setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue());
                taxData[1][2] = buysPiece.getTax2().setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue());
                taxData[2][2] = buysPiece.getTax3().setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue());
                taxData[3][2] = buysPiece.getTax4().setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue());
                taxData[4][2] = buysPiece.getTax5().setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue());
                break;
        }
        getTaxTable().setData(taxData);
    }

    private void initRowsTable() {
        getRowsTable().setTypes(new Class[]{
                    java.lang.Integer.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.math.BigDecimal.class, java.math.BigDecimal.class, java.math.BigDecimal.class, java.math.BigDecimal.class, java.math.BigDecimal.class, javax.swing.Icon.class
                });
        getRowsTable().setListColumnNotEditable(new int[]{
                    0, 7, 8
                });
        getRowsTable().setCellEditable(false);
        getRowsTable().setPreferredColumnSize(new int[]{
                    25, 70, 130, 60, 40, 50, 50, 50, 50, 15
                });
        getRowsTable().setColumnNames(new String[]{
                    "", "R\351f\351rence", "D\351signation", "Lot", "Qt", "Pv/a", "Re", "Pu HT", "Mt HT", "?"
                });
        rowsData = new Object[30][10];
        for (int i = 0; i < rowsData.length; i++) {
            rowsData[i][0] = null;
            rowsData[i][1] = null;
            rowsData[i][2] = null;
            rowsData[i][3] = null;
            rowsData[i][4] = null;
            rowsData[i][5] = null;
            rowsData[i][6] = null;
            rowsData[i][7] = null;
            rowsData[i][8] = null;
            rowsData[i][9] = null;
        }

        showRowsTable();
    }

    private void initRowsValues() {
        if (rowsList != null) {
            Iterator it = rowsList.iterator();
            rowsData = new Object[rowsList.size()][10];
            for (int i = 0; it.hasNext(); i++) {
                orderRow = (OrderRow) it.next();
                rowsData[i][0] = orderRow.getId();
                rowsData[i][1] = orderRow.getArticleId();
                rowsData[i][2] = orderRow.getDesignation();
                rowsData[i][3] = orderRow.getLotId();
                rowsData[i][4] = orderRow.getQuantite().setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue());
                rowsData[i][5] = orderRow.getPrixVente().setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue());
                rowsData[i][6] = orderRow.getRemise().setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue());
                rowsData[i][7] = orderRow.getPrixUnitaire().setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue());
                rowsData[i][8] = orderRow.getTotalHt().setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue());
                if (orderRow.getStatus().compareTo(orderRow.getQuantite()) == 0) {
                    rowsData[i][9] = green;
                } else if (orderRow.getStatus().compareTo(new BigDecimal(0)) == 0) {
                    rowsData[i][9] = red;
                } else {
                    rowsData[i][9] = orange;
                }
            }

        } else {
            rowsData = new Object[30][10];
            for (int i = 0; i < rowsData.length; i++) {
                rowsData[i][0] = null;
                rowsData[i][1] = null;
                rowsData[i][2] = null;
                rowsData[i][3] = null;
                rowsData[i][4] = null;
                rowsData[i][5] = null;
                rowsData[i][6] = null;
                rowsData[i][7] = null;
                rowsData[i][8] = null;
                rowsData[i][9] = null;
            }

        }
        showRowsTable();
    }

    private void showRowsTable() {
        getRowsTable().setData(rowsData);
        model = getRowsTable().getTable().getColumnModel();
        model.getColumn(0).setCellRenderer(labelRenderer);
        getRowsTable().getTable().addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                if (e.getButton() != 1 && write && pieceType < 3 && salesPiece.getBillReference() == null) {
                    getPopupOrderStockRows().show(e.getComponent(), e.getX(), e.getY());
                }
            }
        });
        getRowsTable().getTableModel().addTableModelListener(new TableModelListener() {
            public void tableChanged(TableModelEvent e) {
                if (updateRowsTable) {
                    actionUpdateRow(e.getFirstRow());
                }
            }
        });
    }

    private void initInfoField(String txt) {
        setInfoField(txt);
    }

    private void initDueDateField() {
        if (pieceType == 3 && customer != null) {
            setDueDateField("Ech\351ance : " + salesBillPiece.getDueDate());
        } else {
            setDueDateField("");
        }
    }

    private void initTotalPanel() {
        getTotalPanel().addMouseListener(new MouseClickShipping());
    }

    private void initTotalValues() {
        switch (pieceType) {
            case 0: // '\0'
                getTotalPanel().setTotalBeforeTaxValue(salesPiece.getGrossPrice().setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue()).toString());
                getTotalPanel().setTaxValue(salesPiece.getTotalTax().setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue()).toString());
                getTotalPanel().setShippingValue(salesPiece.getShippingPrice().setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue()).toString());
                getTotalPanel().setTotalPaymentValue(salesPiece.getNetPrice().setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue()).toString());
                getTotalPanel().setPartPaymentValue(salesPiece.getPartPayment().setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue()).toString());
                getTotalPanel().setDueValue(salesPiece.getNetPrice().subtract(salesPiece.getPartPayment()).setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue()).toString());
                break;

            case 1: // '\001'
                getTotalPanel().setTotalBeforeTaxValue(salesPiece.getGrossPrice().setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue()).toString());
                getTotalPanel().setTaxValue(salesPiece.getTotalTax().setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue()).toString());
                getTotalPanel().setShippingValue(salesPiece.getShippingPrice().setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue()).toString());
                getTotalPanel().setTotalPaymentValue(salesPiece.getNetPrice().setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue()).toString());
                getTotalPanel().setPartPaymentValue(salesPiece.getPartPayment().setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue()).toString());
                getTotalPanel().setDueValue(salesPiece.getNetPrice().subtract(salesPiece.getPartPayment()).setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue()).toString());
                break;

            case 2: // '\002'
                getTotalPanel().setTotalBeforeTaxValue(salesPiece.getGrossPrice().setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue()).toString());
                getTotalPanel().setTaxValue(salesPiece.getTotalTax().setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue()).toString());
                getTotalPanel().setShippingValue(salesPiece.getShippingPrice().setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue()).toString());
                getTotalPanel().setTotalPaymentValue(salesPiece.getNetPrice().setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue()).toString());
                getTotalPanel().setPartPaymentValue(salesPiece.getPartPayment().setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue()).toString());
                getTotalPanel().setDueValue(salesPiece.getNetPrice().subtract(salesPiece.getPartPayment()).setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue()).toString());
                break;

            case 3: // '\003'
                getTotalPanel().setTotalBeforeTaxValue(salesBillPiece.getGrossPrice().setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue()).toString());
                getTotalPanel().setTaxValue(salesBillPiece.getTotalTax().setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue()).toString());
                getTotalPanel().setShippingValue(salesBillPiece.getShippingPrice().setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue()).toString());
                getTotalPanel().setTotalPaymentValue(salesBillPiece.getNetPrice().setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue()).toString());
                getTotalPanel().setPartPaymentValue(salesBillPiece.getPartPayment().setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue()).toString());
                getTotalPanel().setDueValue(salesBillPiece.getNetPrice().subtract(salesBillPiece.getPartPayment()).setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue()).toString());
                break;

            case 4: // '\004'
                getTotalPanel().setTotalBeforeTaxValue(salesBillPiece.getGrossPrice().setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue()).toString());
                getTotalPanel().setTaxValue(salesBillPiece.getTotalTax().setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue()).toString());
                getTotalPanel().setShippingValue(salesBillPiece.getShippingPrice().setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue()).toString());
                getTotalPanel().setTotalPaymentValue(salesBillPiece.getNetPrice().setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue()).toString());
                getTotalPanel().setPartPaymentValue(salesBillPiece.getPartPayment().setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue()).toString());
                getTotalPanel().setDueValue(salesBillPiece.getNetPrice().subtract(salesBillPiece.getPartPayment()).setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue()).toString());
                break;

            case 5: // '\005'
                getTotalPanel().setTotalBeforeTaxValue(buysPiece.getGrossPrice().setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue()).toString());
                getTotalPanel().setTaxValue(buysPiece.getTotalTax().setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue()).toString());
                getTotalPanel().setShippingValue(buysPiece.getShippingPrice().setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue()).toString());
                getTotalPanel().setTotalPaymentValue(buysPiece.getNetPrice().setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue()).toString());
                getTotalPanel().setPartPaymentValue(buysPiece.getPartPayment().setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue()).toString());
                getTotalPanel().setDueValue(buysPiece.getNetPrice().subtract(buysPiece.getPartPayment()).setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue()).toString());
                break;

            case 6: // '\006'
                getTotalPanel().setTotalBeforeTaxValue(buysPiece.getGrossPrice().setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue()).toString());
                getTotalPanel().setTaxValue(buysPiece.getTotalTax().setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue()).toString());
                getTotalPanel().setShippingValue(buysPiece.getShippingPrice().setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue()).toString());
                getTotalPanel().setTotalPaymentValue(buysPiece.getNetPrice().setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue()).toString());
                getTotalPanel().setPartPaymentValue(buysPiece.getPartPayment().setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue()).toString());
                getTotalPanel().setDueValue(buysPiece.getNetPrice().subtract(buysPiece.getPartPayment()).setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue()).toString());
                break;
        }
    }

    private void setTaxColumn(JTable table, TableColumn modeColumn) {
        modeColumn.setCellEditor(new DefaultCellEditor(getTaxJComboBox()));
        DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
        renderer.setToolTipText("S\351l\351ctionner une TVA");
        modeColumn.setCellRenderer(renderer);
    }

    private JComboBox getTaxJComboBox() {
        if (taxJComboBox == null) {
            taxJComboBox = new JComboBox();
            taxJComboBox.setModel(new DefaultComboBoxModel(taxList));
        }
        return taxJComboBox;
    }

    private void initModifyOrder() {
        if (modifyOrderCtrl == null) {
            modifyOrderCtrl = new ModifyOrderController(desktop);
            modifyOrderCtrl.getOK().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    modifyOrderCtrl.actionOK();
                    actionModifyOrder(modifyOrderCtrl.getAgency(), modifyOrderCtrl.getSalesPerson());
                }
            });
            modifyOrderCtrl.getCANCEL().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    modifyOrderCtrl.actionCANCEL();
                }
            });
        } else {
            modifyOrderCtrl.init();
        }
    }

    private void initShippingOrder() {
        if (shippingOrderCtrl == null) {
            shippingOrderCtrl = new ShippingOrderController(desktop);
            shippingOrderCtrl.getOK().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    shippingOrderCtrl.actionOK();
                    actionCalculateShipping(shippingOrderCtrl.getSum());
                }
            });
            shippingOrderCtrl.getCANCEL().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    shippingOrderCtrl.actionCANCEL();
                }
            });
        } else {
            shippingOrderCtrl.init();
        }
    }

    private void actionUpdateRow(int row) {
        if (getRowsTable().getTable().getValueAt(row, 8) != null) {
            if (calculateRow) {
                OrderRow oldOrderRow = null;
                OrderRow orderRow = new OrderRow();
                orderRow.setId((Integer) getRowsTable().getTable().getValueAt(row, 0));
                orderRow.setLine(new Integer(row));
                orderRow.setArticleId((String) getRowsTable().getTable().getValueAt(row, 1));
                orderRow.setDesignation((String) getRowsTable().getTable().getValueAt(row, 2));
                orderRow.setLotId((String) getRowsTable().getTable().getValueAt(row, 3));
                orderRow.setQuantite((BigDecimal) getRowsTable().getTable().getValueAt(row, 4));
                orderRow.setPrixVente((BigDecimal) getRowsTable().getTable().getValueAt(row, 5));
                orderRow.setRemise((BigDecimal) getRowsTable().getTable().getValueAt(row, 6));
                orderRow.setPrixUnitaire((BigDecimal) getRowsTable().getTable().getValueAt(row, 7));
                orderRow.setTotalHt((BigDecimal) getRowsTable().getTable().getValueAt(row, 8));
                orderRow.setTvaId(new Integer(String.valueOf(getRowsTable().getTable().getValueAt(row, 9))));
                orderRow.setReliquatId(new Integer(0));
                orderRow.setUserId(param.getUserId());
                int rowsType;
                if (pieceType < 3) {
                    rowsType = 28;
                    orderRow.setVenteId(pieceId);
                } else if (pieceType < 5) {
                    rowsType = 28;
                    orderRow.setFactureId(pieceId);
                } else {
                    rowsType = 30;
                    orderRow.setVenteId(pieceId);
                }
                if (orderRow.getId() == null) {
                    Integer pk = (Integer) crud.create(rowsType, null);
                    updateRowsTable = false;
                    getRowsTable().getTable().setValueAt(pk, row, 0);
                    updateRowsTable = true;
                    orderRow.setId(pk);
                    orderRow.setStatus(new BigDecimal("0.00"));
                    orderRow.setBatchId(new Integer(0));
                    oldOrderRow = (OrderRow) crud.read(pk, rowsType);
                    orderRow.setUpdateLastTime(oldOrderRow.getUpdateLastTime());
                    if (!crud.update(orderRow, rowsType)) {
                        JOptionPane.showMessageDialog(null, "Impossible d'enregistrer les donn\351es !");
                    } else {
                        orderRow = (OrderRow) crud.read(orderRow.getId(), rowsType);
                        actionUpdateOrder(orderRow, null);
                        rowsList.add(orderRow);
                        calculateRow = false;
                    }
                } else {
                    oldOrderRow = (OrderRow) rowsList.get(row);
                    orderRow.setUpdateLastTime(oldOrderRow.getUpdateLastTime());
                    if (!crud.update(orderRow, rowsType)) {
                        JOptionPane.showMessageDialog(null, "Impossible d'enregistrer les donn\351es !\n(un autre utilisateur les a peut-\352tre modifi\351es entre temps)");
                    } else {
                        orderRow = (OrderRow) crud.read(orderRow.getId(), rowsType);
                        actionUpdateOrder(orderRow, oldOrderRow);
                        rowsList.set(row, orderRow);
                        calculateRow = false;
                    }
                }
            } else {
                calculateRow(row);
                calculateRow = true;
                actionUpdateRow(row);
            }
        } else if (getRowsTable().getTable().getValueAt(row, 4) != null && getRowsTable().getTable().getValueAt(row, 5) != null && getRowsTable().getTable().getValueAt(row, 6) != null) {
            calculateRow(row);
            calculateRow = true;
            actionUpdateRow(row);
        }
    }

    private void actionUpdatePreparedRow(OrderRow or) {
        OrderRow orderRow = or;
        if (or.getId() != null && !calculateRow) {
            updateRowsTable = false;
            getRowsTable().getTable().setValueAt(orderRow.getLotId(), orderRow.getLine().intValue(), 3);
            getRowsTable().getTable().setValueAt(orderRow.getQuantite().setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue()), orderRow.getLine().intValue(), 4);
            getRowsTable().getTable().setValueAt(green, or.getLine().intValue(), 9);
            updateRowsTable = true;
        } else if (or.getId() == null && !calculateRow) {
            Object newRowsData[][] = (Object[][]) null;
            newRowsData = new Object[rowsData.length + 1][10];
            for (int i = 0; i < rowsData.length; i++) {
                newRowsData[i][0] = rowsData[i][0];
                newRowsData[i][1] = rowsData[i][1];
                newRowsData[i][2] = rowsData[i][2];
                newRowsData[i][3] = rowsData[i][3];
                newRowsData[i][4] = rowsData[i][4];
                newRowsData[i][5] = rowsData[i][5];
                newRowsData[i][6] = rowsData[i][6];
                newRowsData[i][7] = rowsData[i][7];
                newRowsData[i][8] = rowsData[i][8];
                newRowsData[i][9] = rowsData[i][9];
            }

            for (int i = rowsData.length; i < newRowsData.length; i++) {
                newRowsData[i][0] = null;
                newRowsData[i][1] = null;
                newRowsData[i][2] = null;
                newRowsData[i][3] = null;
                newRowsData[i][4] = null;
                newRowsData[i][5] = null;
                newRowsData[i][6] = null;
                newRowsData[i][7] = null;
                newRowsData[i][8] = null;
                newRowsData[i][9] = null;
            }

            rowsData = newRowsData;
            showRowsTable();
            orderRow.setLine(new Integer(newRowsData.length - 1));
            updateRowsTable = false;
            getRowsTable().getTable().setValueAt(orderRow.getArticleId(), orderRow.getLine().intValue(), 1);
            getRowsTable().getTable().setValueAt(orderRow.getDesignation(), orderRow.getLine().intValue(), 2);
            getRowsTable().getTable().setValueAt(orderRow.getLotId(), orderRow.getLine().intValue(), 3);
            getRowsTable().getTable().setValueAt(orderRow.getQuantite().setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue()), orderRow.getLine().intValue(), 4);
            getRowsTable().getTable().setValueAt(orderRow.getPrixVente().setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue()), orderRow.getLine().intValue(), 5);
            getRowsTable().getTable().setValueAt(orderRow.getRemise().setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue()), orderRow.getLine().intValue(), 6);
            getRowsTable().getTable().setValueAt(orderRow.getPrixUnitaire().setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue()), orderRow.getLine().intValue(), 7);
            getRowsTable().getTable().setValueAt(orderRow.getTotalHt().setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue()), orderRow.getLine().intValue(), 8);
            getRowsTable().getTable().setValueAt(green, orderRow.getLine().intValue(), 9);
            updateRowsTable = true;
        }
        if (calculateRow) {
            OrderRow oldOrderRow = null;
            orderRow.setPrixVente((BigDecimal) getRowsTable().getTable().getValueAt(orderRow.getLine().intValue(), 5));
            orderRow.setRemise((BigDecimal) getRowsTable().getTable().getValueAt(orderRow.getLine().intValue(), 6));
            orderRow.setPrixUnitaire((BigDecimal) getRowsTable().getTable().getValueAt(orderRow.getLine().intValue(), 7));
            orderRow.setTotalHt((BigDecimal) getRowsTable().getTable().getValueAt(orderRow.getLine().intValue(), 8));
            if (orderRow.getId() == null) {
                Integer pk = (Integer) crud.create(28, null);
                updateRowsTable = false;
                updateRowsTable = true;
                orderRow.setId(pk);
                oldOrderRow = (OrderRow) crud.read(pk, 28);
                orderRow.setUpdateLastTime(oldOrderRow.getUpdateLastTime());
                if (!crud.update(orderRow, 28)) {
                    JOptionPane.showMessageDialog(null, "Impossible d'enregistrer les donn\351es !");
                } else {
                    orderRow = (OrderRow) crud.read(orderRow.getId(), 28);
                    actionUpdateOrder(orderRow, null);
                    rowsList.add(orderRow);
                    calculateRow = false;
                }
            } else {
                oldOrderRow = (OrderRow) rowsList.get(orderRow.getLine().intValue());
                orderRow.setUpdateLastTime(oldOrderRow.getUpdateLastTime());
                if (!crud.update(orderRow, 28)) {
                    JOptionPane.showMessageDialog(null, "Impossible d'enregistrer les donn\351es !\n(un autre utilisateur les a peut-\352tre modifi\351es entre temps)");
                } else {
                    orderRow = (OrderRow) crud.read(orderRow.getId(), 28);
                    actionUpdateOrder(orderRow, oldOrderRow);
                    rowsList.set(orderRow.getLine().intValue(), orderRow);
                    calculateRow = false;
                }
            }
        } else {
            calculateRow(orderRow.getLine().intValue());
            calculateRow = true;
            actionUpdatePreparedRow(orderRow);
        }
    }

    private void actionUpdateReceptionRow(OrderRow or) {
        OrderRow orderRow = or;
        if (or.getId() != null && !calculateRow) {
            updateRowsTable = false;
            getRowsTable().getTable().setValueAt(orderRow.getLotId(), orderRow.getLine().intValue(), 3);
            getRowsTable().getTable().setValueAt(orderRow.getQuantite().setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue()), orderRow.getLine().intValue(), 4);
            getRowsTable().getTable().setValueAt(green, or.getLine().intValue(), 9);
            updateRowsTable = true;
        } else if (or.getId() == null && !calculateRow) {
            Object newRowsData[][] = (Object[][]) null;
            newRowsData = new Object[rowsData.length + 1][10];
            for (int i = 0; i < rowsData.length; i++) {
                newRowsData[i][0] = rowsData[i][0];
                newRowsData[i][1] = rowsData[i][1];
                newRowsData[i][2] = rowsData[i][2];
                newRowsData[i][3] = rowsData[i][3];
                newRowsData[i][4] = rowsData[i][4];
                newRowsData[i][5] = rowsData[i][5];
                newRowsData[i][6] = rowsData[i][6];
                newRowsData[i][7] = rowsData[i][7];
                newRowsData[i][8] = rowsData[i][8];
                newRowsData[i][9] = rowsData[i][9];
            }

            for (int i = rowsData.length; i < newRowsData.length; i++) {
                newRowsData[i][0] = null;
                newRowsData[i][1] = null;
                newRowsData[i][2] = null;
                newRowsData[i][3] = null;
                newRowsData[i][4] = null;
                newRowsData[i][5] = null;
                newRowsData[i][6] = null;
                newRowsData[i][7] = null;
                newRowsData[i][8] = null;
                newRowsData[i][9] = null;
            }

            rowsData = newRowsData;
            showRowsTable();
            orderRow.setLine(new Integer(newRowsData.length - 1));
            updateRowsTable = false;
            getRowsTable().getTable().setValueAt(orderRow.getArticleId(), orderRow.getLine().intValue(), 1);
            getRowsTable().getTable().setValueAt(orderRow.getDesignation(), orderRow.getLine().intValue(), 2);
            getRowsTable().getTable().setValueAt(orderRow.getLotId(), orderRow.getLine().intValue(), 3);
            getRowsTable().getTable().setValueAt(orderRow.getQuantite().setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue()), orderRow.getLine().intValue(), 4);
            getRowsTable().getTable().setValueAt(orderRow.getPrixVente().setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue()), orderRow.getLine().intValue(), 5);
            getRowsTable().getTable().setValueAt(orderRow.getRemise().setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue()), orderRow.getLine().intValue(), 6);
            getRowsTable().getTable().setValueAt(orderRow.getPrixUnitaire().setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue()), orderRow.getLine().intValue(), 7);
            getRowsTable().getTable().setValueAt(orderRow.getTotalHt().setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue()), orderRow.getLine().intValue(), 8);
            getRowsTable().getTable().setValueAt(green, orderRow.getLine().intValue(), 9);
            updateRowsTable = true;
        }
        if (calculateRow) {
            OrderRow oldOrderRow = null;
            orderRow.setPrixVente((BigDecimal) getRowsTable().getTable().getValueAt(orderRow.getLine().intValue(), 5));
            orderRow.setRemise((BigDecimal) getRowsTable().getTable().getValueAt(orderRow.getLine().intValue(), 6));
            orderRow.setPrixUnitaire((BigDecimal) getRowsTable().getTable().getValueAt(orderRow.getLine().intValue(), 7));
            orderRow.setTotalHt((BigDecimal) getRowsTable().getTable().getValueAt(orderRow.getLine().intValue(), 8));
            if (orderRow.getId() == null) {
                Integer pk = (Integer) crud.create(30, null);
                updateRowsTable = false;
                updateRowsTable = true;
                orderRow.setId(pk);
                oldOrderRow = (OrderRow) crud.read(pk, 30);
                orderRow.setUpdateLastTime(oldOrderRow.getUpdateLastTime());
                if (!crud.update(orderRow, 30)) {
                    JOptionPane.showMessageDialog(null, "Impossible d'enregistrer les donn\351es !");
                } else {
                    orderRow = (OrderRow) crud.read(orderRow.getId(), 30);
                    actionUpdateOrder(orderRow, null);
                    rowsList.add(orderRow);
                    calculateRow = false;
                }
            } else {
                int row = orderRow.getLine().intValue();
                oldOrderRow = (OrderRow) rowsList.get(row);
                orderRow.setUpdateLastTime(oldOrderRow.getUpdateLastTime());
                if (!crud.update(orderRow, 30)) {
                    JOptionPane.showMessageDialog(null, "Impossible d'enregistrer les donn\351es !\n(un autre utilisateur les a peut-\352tre modifi\351es entre temps)");
                } else {
                    orderRow = (OrderRow) crud.read(orderRow.getId(), 30);
                    actionUpdateOrder(orderRow, oldOrderRow);
                    rowsList.set(row, orderRow);
                    calculateRow = false;
                }
            }
        } else {
            calculateRow(orderRow.getLine().intValue());
            calculateRow = true;
            actionUpdateReceptionRow(orderRow);
        }
    }

    private void calculateRow(int row) {
        BigDecimal qt = (BigDecimal) getRowsTable().getTable().getValueAt(row, 4);
        BigDecimal pv = (BigDecimal) getRowsTable().getTable().getValueAt(row, 5);
        BigDecimal re = (BigDecimal) getRowsTable().getTable().getValueAt(row, 6);
        BigDecimal pu = pv.subtract(pv.multiply(re.movePointLeft(2))).setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue());
        BigDecimal tht = pu.multiply(qt).setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue());
        updateRowsTable = false;
        getRowsTable().getTable().setValueAt(pu, row, 7);
        getRowsTable().getTable().setValueAt(tht, row, 8);
        updateRowsTable = true;
        rowsData = getRowsTable().getData();
        showRowsTable();
    }

    private void actionUpdateOrder(OrderRow newRow, OrderRow oldRow) {
        if (oldRow == null) {
            addValuesRow(newRow);
        } else {
            substractValuesRow(oldRow);
            addValuesRow(newRow);
        }
        actionUpdatePiece();
        initTotalValues();
    }

    private void substractValuesRow(OrderRow or) {
        if (pieceType < 3) {
            salesPiece.setGrossPrice(salesPiece.getGrossPrice().subtract(or.getTotalHt()));
            switch (or.getTvaId().intValue()) {
                case 1: // '\001'
                {
                    BigDecimal rate = (BigDecimal) taxData[0][1];
                    BigDecimal tax = rate.movePointLeft(2).multiply(or.getTotalHt()).setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue());
                    salesPiece.setTax1(salesPiece.getTax1().subtract(tax).setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue()));
                    taxData[0][2] = salesPiece.getTax1();
                    break;
                }

                case 2: // '\002'
                {
                    BigDecimal rate = (BigDecimal) taxData[1][1];
                    BigDecimal tax = rate.movePointLeft(2).multiply(or.getTotalHt()).setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue());
                    salesPiece.setTax2(salesPiece.getTax2().subtract(tax).setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue()));
                    taxData[1][2] = salesPiece.getTax2();
                    break;
                }

                case 3: // '\003'
                {
                    BigDecimal rate = (BigDecimal) taxData[2][1];
                    BigDecimal tax = rate.movePointLeft(2).multiply(or.getTotalHt()).setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue());
                    salesPiece.setTax3(salesPiece.getTax3().subtract(tax).setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue()));
                    taxData[2][2] = salesPiece.getTax3();
                    break;
                }

                case 4: // '\004'
                {
                    BigDecimal rate = (BigDecimal) taxData[3][1];
                    BigDecimal tax = rate.movePointLeft(2).multiply(or.getTotalHt()).setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue());
                    salesPiece.setTax4(salesPiece.getTax4().subtract(tax).setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue()));
                    taxData[3][2] = salesPiece.getTax4();
                    break;
                }

                case 5: // '\005'
                {
                    BigDecimal rate = (BigDecimal) taxData[4][1];
                    BigDecimal tax = rate.movePointLeft(2).multiply(or.getTotalHt()).setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue());
                    salesPiece.setTax5(salesPiece.getTax5().subtract(tax).setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue()));
                    taxData[4][2] = salesPiece.getTax5();
                    break;
                }
            }
            salesPiece.setTotalTax(salesPiece.getTax1().add(salesPiece.getTax2().add(salesPiece.getTax3().add(salesPiece.getTax4().add(salesPiece.getTax5().add(salesPiece.getShippingTax()))))));
            salesPiece.setNetPrice(salesPiece.getGrossPrice().add(salesPiece.getShippingPrice().add(salesPiece.getTotalTax().add(salesPiece.getShippingPrice()))));
        } else if (pieceType < 5) {
            salesBillPiece.setGrossPrice(salesBillPiece.getGrossPrice().subtract(or.getTotalHt()));
            switch (or.getTvaId().intValue()) {
                case 1: // '\001'
                {
                    BigDecimal rate = (BigDecimal) taxData[0][1];
                    BigDecimal tax = rate.movePointLeft(2).multiply(or.getTotalHt()).setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue());
                    salesBillPiece.setTax1(salesBillPiece.getTax1().subtract(tax).setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue()));
                    taxData[0][2] = salesBillPiece.getTax1();
                    break;
                }

                case 2: // '\002'
                {
                    BigDecimal rate = (BigDecimal) taxData[1][1];
                    BigDecimal tax = rate.movePointLeft(2).multiply(or.getTotalHt()).setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue());
                    salesBillPiece.setTax2(salesBillPiece.getTax2().subtract(tax).setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue()));
                    taxData[1][2] = salesBillPiece.getTax2();
                    break;
                }

                case 3: // '\003'
                {
                    BigDecimal rate = (BigDecimal) taxData[2][1];
                    BigDecimal tax = rate.movePointLeft(2).multiply(or.getTotalHt()).setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue());
                    salesBillPiece.setTax3(salesBillPiece.getTax3().subtract(tax).setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue()));
                    taxData[2][2] = salesBillPiece.getTax3();
                    break;
                }

                case 4: // '\004'
                {
                    BigDecimal rate = (BigDecimal) taxData[3][1];
                    BigDecimal tax = rate.movePointLeft(2).multiply(or.getTotalHt()).setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue());
                    salesBillPiece.setTax4(salesBillPiece.getTax4().subtract(tax).setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue()));
                    taxData[3][2] = salesBillPiece.getTax4();
                    break;
                }

                case 5: // '\005'
                {
                    BigDecimal rate = (BigDecimal) taxData[4][1];
                    BigDecimal tax = rate.movePointLeft(2).multiply(or.getTotalHt()).setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue());
                    salesBillPiece.setTax5(salesBillPiece.getTax5().subtract(tax).setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue()));
                    taxData[4][2] = salesBillPiece.getTax5();
                    break;
                }
            }
            salesBillPiece.setTotalTax(salesBillPiece.getTax1().add(salesBillPiece.getTax2().add(salesBillPiece.getTax3().add(salesBillPiece.getTax4().add(salesBillPiece.getTax5().add(salesBillPiece.getShippingTax()))))));
            salesBillPiece.setNetPrice(salesBillPiece.getGrossPrice().add(salesBillPiece.getShippingPrice().add(salesBillPiece.getTotalTax().add(salesBillPiece.getShippingPrice()))));
        } else {
            buysPiece.setGrossPrice(buysPiece.getGrossPrice().subtract(or.getTotalHt()));
            switch (or.getTvaId().intValue()) {
                case 1: // '\001'
                {
                    BigDecimal rate = (BigDecimal) taxData[0][1];
                    BigDecimal tax = rate.movePointLeft(2).multiply(or.getTotalHt()).setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue());
                    buysPiece.setTax1(buysPiece.getTax1().subtract(tax).setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue()));
                    taxData[0][2] = buysPiece.getTax1();
                    break;
                }

                case 2: // '\002'
                {
                    BigDecimal rate = (BigDecimal) taxData[1][1];
                    BigDecimal tax = rate.movePointLeft(2).multiply(or.getTotalHt()).setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue());
                    buysPiece.setTax2(buysPiece.getTax2().subtract(tax).setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue()));
                    taxData[1][2] = buysPiece.getTax2();
                    break;
                }

                case 3: // '\003'
                {
                    BigDecimal rate = (BigDecimal) taxData[2][1];
                    BigDecimal tax = rate.movePointLeft(2).multiply(or.getTotalHt()).setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue());
                    buysPiece.setTax3(buysPiece.getTax3().subtract(tax).setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue()));
                    taxData[2][2] = buysPiece.getTax3();
                    break;
                }

                case 4: // '\004'
                {
                    BigDecimal rate = (BigDecimal) taxData[3][1];
                    BigDecimal tax = rate.movePointLeft(2).multiply(or.getTotalHt()).setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue());
                    buysPiece.setTax4(buysPiece.getTax4().subtract(tax).setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue()));
                    taxData[3][2] = buysPiece.getTax4();
                    break;
                }

                case 5: // '\005'
                {
                    BigDecimal rate = (BigDecimal) taxData[4][1];
                    BigDecimal tax = rate.movePointLeft(2).multiply(or.getTotalHt()).setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue());
                    buysPiece.setTax5(buysPiece.getTax5().subtract(tax).setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue()));
                    taxData[4][2] = buysPiece.getTax5();
                    break;
                }
            }
            buysPiece.setTotalTax(buysPiece.getTax1().add(buysPiece.getTax2().add(buysPiece.getTax3().add(buysPiece.getTax4().add(buysPiece.getTax5().add(buysPiece.getShippingTax()))))));
            buysPiece.setNetPrice(buysPiece.getGrossPrice().add(buysPiece.getShippingPrice().add(buysPiece.getTotalTax().add(buysPiece.getShippingPrice()))));
        }
    }

    private void addValuesRow(OrderRow or) {
        if (pieceType < 3) {
            salesPiece.setGrossPrice(salesPiece.getGrossPrice().add(or.getTotalHt()));
            switch (or.getTvaId().intValue()) {
                case 1: // '\001'
                {
                    BigDecimal rate = (BigDecimal) taxData[0][1];
                    BigDecimal tax = rate.movePointLeft(2).multiply(or.getTotalHt()).setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue());
                    salesPiece.setTax1(salesPiece.getTax1().add(tax).setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue()));
                    taxData[0][2] = salesPiece.getTax1();
                    break;
                }

                case 2: // '\002'
                {
                    BigDecimal rate = (BigDecimal) taxData[1][1];
                    BigDecimal tax = rate.movePointLeft(2).multiply(or.getTotalHt()).setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue());
                    salesPiece.setTax2(salesPiece.getTax2().add(tax).setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue()));
                    taxData[1][2] = salesPiece.getTax2();
                    break;
                }

                case 3: // '\003'
                {
                    BigDecimal rate = (BigDecimal) taxData[2][1];
                    BigDecimal tax = rate.movePointLeft(2).multiply(or.getTotalHt()).setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue());
                    salesPiece.setTax3(salesPiece.getTax3().add(tax).setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue()));
                    taxData[2][2] = salesPiece.getTax3();
                    break;
                }

                case 4: // '\004'
                {
                    BigDecimal rate = (BigDecimal) taxData[3][1];
                    BigDecimal tax = rate.movePointLeft(2).multiply(or.getTotalHt()).setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue());
                    salesPiece.setTax4(salesPiece.getTax4().add(tax).setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue()));
                    taxData[3][2] = salesPiece.getTax4();
                    break;
                }

                case 5: // '\005'
                {
                    BigDecimal rate = (BigDecimal) taxData[4][1];
                    BigDecimal tax = rate.movePointLeft(2).multiply(or.getTotalHt()).setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue());
                    salesPiece.setTax5(salesPiece.getTax5().add(tax).setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue()));
                    taxData[4][2] = salesPiece.getTax5();
                    break;
                }
            }
            getTaxTable().setData(taxData);
            salesPiece.setTotalTax(salesPiece.getTax1().add(salesPiece.getTax2().add(salesPiece.getTax3().add(salesPiece.getTax4().add(salesPiece.getTax5().add(salesPiece.getShippingTax()))))));
            salesPiece.setNetPrice(salesPiece.getGrossPrice().add(salesPiece.getShippingPrice().add(salesPiece.getTotalTax().add(salesPiece.getShippingPrice()))));
        } else if (pieceType < 5) {
            salesBillPiece.setGrossPrice(salesBillPiece.getGrossPrice().add(or.getTotalHt()));
            switch (or.getTvaId().intValue()) {
                case 1: // '\001'
                {
                    BigDecimal rate = (BigDecimal) taxData[0][1];
                    BigDecimal tax = rate.movePointLeft(2).multiply(or.getTotalHt()).setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue());
                    salesBillPiece.setTax1(salesBillPiece.getTax1().add(tax).setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue()));
                    taxData[0][2] = salesBillPiece.getTax1();
                    break;
                }

                case 2: // '\002'
                {
                    BigDecimal rate = (BigDecimal) taxData[1][1];
                    BigDecimal tax = rate.movePointLeft(2).multiply(or.getTotalHt()).setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue());
                    salesBillPiece.setTax2(salesBillPiece.getTax2().add(tax).setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue()));
                    taxData[1][2] = salesBillPiece.getTax2();
                    break;
                }

                case 3: // '\003'
                {
                    BigDecimal rate = (BigDecimal) taxData[2][1];
                    BigDecimal tax = rate.movePointLeft(2).multiply(or.getTotalHt()).setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue());
                    salesBillPiece.setTax3(salesBillPiece.getTax3().add(tax).setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue()));
                    taxData[2][2] = salesBillPiece.getTax3();
                    break;
                }

                case 4: // '\004'
                {
                    BigDecimal rate = (BigDecimal) taxData[3][1];
                    BigDecimal tax = rate.movePointLeft(2).multiply(or.getTotalHt()).setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue());
                    salesBillPiece.setTax4(salesBillPiece.getTax4().add(tax).setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue()));
                    taxData[3][2] = salesBillPiece.getTax4();
                    break;
                }

                case 5: // '\005'
                {
                    BigDecimal rate = (BigDecimal) taxData[4][1];
                    BigDecimal tax = rate.movePointLeft(2).multiply(or.getTotalHt()).setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue());
                    salesBillPiece.setTax5(salesBillPiece.getTax5().add(tax).setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue()));
                    taxData[4][2] = salesBillPiece.getTax5();
                    break;
                }
            }
            getTaxTable().setData(taxData);
            salesBillPiece.setTotalTax(salesBillPiece.getTax1().add(salesBillPiece.getTax2().add(salesBillPiece.getTax3().add(salesBillPiece.getTax4().add(salesBillPiece.getTax5().add(salesBillPiece.getShippingTax()))))));
            salesBillPiece.setNetPrice(salesBillPiece.getGrossPrice().add(salesBillPiece.getShippingPrice().add(salesBillPiece.getTotalTax().add(salesBillPiece.getShippingPrice()))));
        } else {
            buysPiece.setGrossPrice(buysPiece.getGrossPrice().add(or.getTotalHt()));
            switch (or.getTvaId().intValue()) {
                case 1: // '\001'
                {
                    BigDecimal rate = (BigDecimal) taxData[0][1];
                    BigDecimal tax = rate.movePointLeft(2).multiply(or.getTotalHt()).setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue());
                    buysPiece.setTax1(buysPiece.getTax1().add(tax).setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue()));
                    taxData[0][2] = buysPiece.getTax1();
                    break;
                }

                case 2: // '\002'
                {
                    BigDecimal rate = (BigDecimal) taxData[1][1];
                    BigDecimal tax = rate.movePointLeft(2).multiply(or.getTotalHt()).setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue());
                    buysPiece.setTax2(buysPiece.getTax2().add(tax).setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue()));
                    taxData[1][2] = buysPiece.getTax2();
                    break;
                }

                case 3: // '\003'
                {
                    BigDecimal rate = (BigDecimal) taxData[2][1];
                    BigDecimal tax = rate.movePointLeft(2).multiply(or.getTotalHt()).setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue());
                    buysPiece.setTax3(buysPiece.getTax3().add(tax).setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue()));
                    taxData[2][2] = buysPiece.getTax3();
                    break;
                }

                case 4: // '\004'
                {
                    BigDecimal rate = (BigDecimal) taxData[3][1];
                    BigDecimal tax = rate.movePointLeft(2).multiply(or.getTotalHt()).setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue());
                    buysPiece.setTax4(buysPiece.getTax4().add(tax).setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue()));
                    taxData[3][2] = buysPiece.getTax4();
                    break;
                }

                case 5: // '\005'
                {
                    BigDecimal rate = (BigDecimal) taxData[4][1];
                    BigDecimal tax = rate.movePointLeft(2).multiply(or.getTotalHt()).setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue());
                    buysPiece.setTax5(buysPiece.getTax5().add(tax).setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue()));
                    taxData[4][2] = buysPiece.getTax5();
                    break;
                }
            }
            getTaxTable().setData(taxData);
            buysPiece.setTotalTax(buysPiece.getTax1().add(buysPiece.getTax2().add(buysPiece.getTax3().add(buysPiece.getTax4().add(buysPiece.getTax5().add(buysPiece.getShippingTax()))))));
            buysPiece.setNetPrice(buysPiece.getGrossPrice().add(buysPiece.getShippingPrice().add(buysPiece.getTotalTax().add(buysPiece.getShippingPrice()))));
        }
    }

    private void actionUpdatePiece() {
        if (salesPiece != null) {
            if (!crud.update(salesPiece, 22)) {
                JOptionPane.showMessageDialog(null, "Impossible d'enregistrer les donn\351es !\n(un autre utilisateur les a peut-\352tre modifi\351es entre temps)");
            } else {
                salesPiece = (SalesPiece) crud.read(salesPiece.getId(), 22);
            }
        } else if (salesBillPiece != null) {
            if (!crud.update(salesBillPiece, 25)) {
                JOptionPane.showMessageDialog(null, "Impossible d'enregistrer les donn\351es !\n(un autre utilisateur les a peut-\352tre modifi\351es entre temps)");
            } else {
                salesBillPiece = (SalesBillPiece) crud.read(salesBillPiece.getId(), 25);
            }
        } else if (buysPiece != null) {
            if (!crud.update(buysPiece, 24)) {
                JOptionPane.showMessageDialog(null, "Impossible d'enregistrer les donn\351es !\n(un autre utilisateur les a peut-\352tre modifi\351es entre temps)");
            } else {
                buysPiece = (BuysPiece) crud.read(buysPiece.getId(), 24);
            }
        }
    }

    private void actionNewLines() {
        Object newRowsData[][] = (Object[][]) null;
        newRowsData = new Object[rowsData.length + 1][10];
        for (int i = 0; i < rowsData.length; i++) {
            newRowsData[i][0] = rowsData[i][0];
            newRowsData[i][1] = rowsData[i][1];
            newRowsData[i][2] = rowsData[i][2];
            newRowsData[i][3] = rowsData[i][3];
            newRowsData[i][4] = rowsData[i][4];
            newRowsData[i][5] = rowsData[i][5];
            newRowsData[i][6] = rowsData[i][6];
            newRowsData[i][7] = rowsData[i][7];
            newRowsData[i][8] = rowsData[i][8];
            newRowsData[i][9] = rowsData[i][9];
        }

        for (int i = rowsData.length; i < newRowsData.length; i++) {
            newRowsData[i][0] = null;
            newRowsData[i][1] = null;
            newRowsData[i][2] = null;
            newRowsData[i][3] = null;
            newRowsData[i][4] = null;
            newRowsData[i][5] = null;
            newRowsData[i][6] = null;
            newRowsData[i][7] = null;
            newRowsData[i][8] = null;
            newRowsData[i][9] = null;
        }

        rowsData = newRowsData;
        showRowsTable();
    }

    private void actionCancelPreparation() {
        int row = getRowsTable().getTable().getSelectedRow();
        if (getRowsTable().getTable().getValueAt(row, 0) != null) {
            OrderRow or = (OrderRow) rowsList.get(row);
            if (or.getQuantite().compareTo(or.getStatus()) != 0) {
                JOptionPane.showMessageDialog(null, "Op\351ration refus\351e !\nCette ligne n'est pas pr\351par\351e.");
                return;
            }
            Batch batch = (Batch) crud.read(or.getBatchId(), 31);
            if (batch != null) {
                batch.setStockQuantity(batch.getStockQuantity().add(or.getQuantite()));
                batch.setUserId(param.getUserId());
                crud.update(batch, 31);
                Integer id = (Integer) crud.create(32, null);
                LogStock logStock = (LogStock) crud.read(id, 32);
                if (logStock != null) {
                    logStock.setBatchId(batch.getId());
                    logStock.setDate((new MEDate()).getPlain());
                    logStock.setDe("C/BL " + or.getVenteId());
                    logStock.setItemId(batch.getItemId());
                    logStock.setNotes("Erreur pr\351p (n\260 lot : " + batch.getItemBatch() + ")");
                    logStock.setOperation("ENTREE");
                    logStock.setQuantity(or.getQuantite());
                    logStock.setUserId(param.getUserId());
                    logStock.setVers("STOCK Agence " + batch.getAgencyId().toString());
                    crud.update(logStock, 32);
                }
                or.setLotId("");
                or.setBatchId(new Integer(0));
                or.setStatus(new BigDecimal("0.00"));
                crud.update(or, 28);
                or = (OrderRow) crud.read(or.getId(), 28);
                updateRowsTable = false;
                getRowsTable().getTable().setValueAt(or.getLotId(), row, 3);
                getRowsTable().getTable().setValueAt(red, row, 9);
                updateRowsTable = true;
                rowsList.set(row, or);
                actionUpdatePiece();
                initTotalValues();
            }
        }
    }

    private void actionDeleteLine() {
        int row = getRowsTable().getTable().getSelectedRow();
        if (getRowsTable().getTable().getValueAt(row, 0) != null) {
            OrderRow or = (OrderRow) rowsList.get(row);
            if (or.getQuantite().compareTo(or.getStatus()) == 0) {
                JOptionPane.showMessageDialog(null, "Op\351ration refus\351e !\nAnnuler la pr\351paration avant de suprimer la ligne.");
                return;
            }
            substractValuesRow(or);
            int rowsType;
            if (pieceType < 3) {
                rowsType = 28;
            } else if (pieceType < 5) {
                rowsType = 29;
            } else {
                rowsType = 30;
            }
            crud.delete(or.getId(), or.getUpdateLastTime(), rowsType);
            rowsList.remove(row);
            actionUpdatePiece();
            initTotalValues();
            actionRemoveRowsData(row);
            actionNewIndexRow();
        }
    }

    private void actionNewIndexRow() {
        for (int i = 0; i < rowsList.size(); i++) {
            OrderRow or = (OrderRow) rowsList.get(i);
            or.setLine(new Integer(i));
            crud.update(or, 28);
            or = (OrderRow) crud.read(or.getId(), 28);
            rowsList.set(i, or);
        }

    }

    private void actionRemoveRowsData(int row) {
        Object newRowsData[][] = new Object[rowsData.length - 1][10];
        for (int i = 0; i < rowsData.length; i++) {
            if (i < row) {
                newRowsData[i][0] = rowsData[i][0];
                newRowsData[i][1] = rowsData[i][1];
                newRowsData[i][2] = rowsData[i][2];
                newRowsData[i][3] = rowsData[i][3];
                newRowsData[i][4] = rowsData[i][4];
                newRowsData[i][5] = rowsData[i][5];
                newRowsData[i][6] = rowsData[i][6];
                newRowsData[i][7] = rowsData[i][7];
                newRowsData[i][8] = rowsData[i][8];
                newRowsData[i][9] = rowsData[i][9];
            } else if (i > row) {
                newRowsData[i - 1][0] = rowsData[i][0];
                newRowsData[i - 1][1] = rowsData[i][1];
                newRowsData[i - 1][2] = rowsData[i][2];
                newRowsData[i - 1][3] = rowsData[i][3];
                newRowsData[i - 1][4] = rowsData[i][4];
                newRowsData[i - 1][5] = rowsData[i][5];
                newRowsData[i - 1][6] = rowsData[i][6];
                newRowsData[i - 1][7] = rowsData[i][7];
                newRowsData[i - 1][8] = rowsData[i][8];
                newRowsData[i - 1][9] = rowsData[i][9];
            }
        }

        rowsData = newRowsData;
        showRowsTable();
    }

    private void actionModifyOrder(Agency agency, SalesPerson salesPerson) {
        this.agency = agency;
        this.salesPerson = salesPerson;
        initAgencyValues();
        initReferencesValues();
        actionUpdatePiece();
    }

    private void actionCalculateShipping(BigDecimal shippingValue) {
        BigDecimal zero = new BigDecimal("0.00");
        if (salesPiece != null) {
            if (salesPiece.getShippingPrice().compareTo(zero) == 0) {
                addShippingValues(shippingValue);
            } else {
                substractShippingValues(salesPiece.getShippingPrice());
                addShippingValues(shippingValue);
            }
        } else if (salesBillPiece != null) {
            if (salesBillPiece.getShippingPrice().compareTo(zero) == 0) {
                addShippingValues(shippingValue);
            } else {
                substractShippingValues(salesBillPiece.getShippingPrice());
                addShippingValues(shippingValue);
            }
        } else if (buysPiece != null) {
            if (buysPiece.getShippingPrice().compareTo(zero) == 0) {
                addShippingValues(shippingValue);
            } else {
                substractShippingValues(buysPiece.getShippingPrice());
                addShippingValues(shippingValue);
            }
        }
        actionUpdatePiece();
        initTotalValues();
    }

    private void substractShippingValues(BigDecimal oldValue) {
        if (pieceType < 3) {
            salesPiece.setShippingPrice(new BigDecimal("0.00"));
            BigDecimal rate = (BigDecimal) taxData[0][1];
            BigDecimal tax = rate.movePointLeft(2).multiply(oldValue).setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue());
            salesPiece.setTax1(salesPiece.getTax1().subtract(tax).setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue()));
            taxData[0][2] = salesPiece.getTax1();
            salesPiece.setTotalTax(salesPiece.getTax1().add(salesPiece.getTax2().add(salesPiece.getTax3().add(salesPiece.getTax4().add(salesPiece.getTax5().add(salesPiece.getShippingTax()))))));
            salesPiece.setNetPrice(salesPiece.getGrossPrice().add(salesPiece.getShippingPrice().add(salesPiece.getTotalTax().add(salesPiece.getShippingPrice()))));
        } else if (pieceType < 5) {
            salesBillPiece.setShippingPrice(new BigDecimal("0.00"));
            BigDecimal rate = (BigDecimal) taxData[0][1];
            BigDecimal tax = rate.movePointLeft(2).multiply(oldValue).setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue());
            salesBillPiece.setTax1(salesBillPiece.getTax1().subtract(tax).setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue()));
            taxData[0][2] = salesBillPiece.getTax1();
            salesBillPiece.setTotalTax(salesBillPiece.getTax1().add(salesBillPiece.getTax2().add(salesBillPiece.getTax3().add(salesBillPiece.getTax4().add(salesBillPiece.getTax5().add(salesBillPiece.getShippingTax()))))));
            salesBillPiece.setNetPrice(salesBillPiece.getGrossPrice().add(salesBillPiece.getShippingPrice().add(salesBillPiece.getTotalTax().add(salesBillPiece.getShippingPrice()))));
        } else {
            buysPiece.setShippingPrice(new BigDecimal("0.00"));
            BigDecimal rate = (BigDecimal) taxData[0][1];
            BigDecimal tax = rate.movePointLeft(2).multiply(oldValue).setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue());
            buysPiece.setTax1(buysPiece.getTax1().subtract(tax).setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue()));
            taxData[0][2] = buysPiece.getTax1();
            buysPiece.setTotalTax(buysPiece.getTax1().add(buysPiece.getTax2().add(buysPiece.getTax3().add(buysPiece.getTax4().add(buysPiece.getTax5().add(buysPiece.getShippingTax()))))));
            buysPiece.setNetPrice(buysPiece.getGrossPrice().add(buysPiece.getShippingPrice().add(buysPiece.getTotalTax().add(buysPiece.getShippingPrice()))));
        }
    }

    private void addShippingValues(BigDecimal value) {
        if (pieceType < 3) {
            salesPiece.setShippingPrice(value);
            BigDecimal rate = (BigDecimal) taxData[0][1];
            BigDecimal tax = rate.movePointLeft(2).multiply(value).setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue());
            salesPiece.setTax1(salesPiece.getTax1().add(tax).setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue()));
            taxData[0][2] = salesPiece.getTax1();
            getTaxTable().setData(taxData);
            salesPiece.setTotalTax(salesPiece.getTax1().add(salesPiece.getTax2().add(salesPiece.getTax3().add(salesPiece.getTax4().add(salesPiece.getTax5().add(salesPiece.getShippingTax()))))));
            salesPiece.setNetPrice(salesPiece.getGrossPrice().add(salesPiece.getShippingPrice().add(salesPiece.getTotalTax().add(salesPiece.getShippingPrice()))));
        } else if (pieceType < 5) {
            salesBillPiece.setShippingPrice(value);
            BigDecimal rate = (BigDecimal) taxData[0][1];
            BigDecimal tax = rate.movePointLeft(2).multiply(value).setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue());
            salesBillPiece.setTax1(salesBillPiece.getTax1().add(tax).setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue()));
            taxData[0][2] = salesBillPiece.getTax1();
            getTaxTable().setData(taxData);
            salesBillPiece.setTotalTax(salesBillPiece.getTax1().add(salesBillPiece.getTax2().add(salesBillPiece.getTax3().add(salesBillPiece.getTax4().add(salesBillPiece.getTax5().add(salesBillPiece.getShippingTax()))))));
            salesBillPiece.setNetPrice(salesBillPiece.getGrossPrice().add(salesBillPiece.getShippingPrice().add(salesBillPiece.getTotalTax().add(salesBillPiece.getShippingPrice()))));
        } else {
            buysPiece.setShippingPrice(value);
            BigDecimal rate = (BigDecimal) taxData[0][1];
            BigDecimal tax = rate.movePointLeft(2).multiply(value).setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue());
            buysPiece.setTax1(buysPiece.getTax1().add(tax).setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue()));
            taxData[0][2] = buysPiece.getTax1();
            getTaxTable().setData(taxData);
            buysPiece.setTotalTax(buysPiece.getTax1().add(buysPiece.getTax2().add(buysPiece.getTax3().add(buysPiece.getTax4().add(buysPiece.getTax5().add(buysPiece.getShippingTax()))))));
            buysPiece.setNetPrice(buysPiece.getGrossPrice().add(buysPiece.getShippingPrice().add(buysPiece.getTotalTax().add(buysPiece.getShippingPrice()))));
        }
    }

    private void actionUpdatePayment(int oldPieceType) {
        String prefix;
        switch (pieceType) {
            case 0: // '\0'
                prefix = "D-CL";
                break;

            case 1: // '\001'
                prefix = "C-CL";
                break;

            case 2: // '\002'
                prefix = "BL-CL";
                break;

            case 3: // '\003'
                prefix = "F-CL";
                break;

            case 4: // '\004'
                prefix = "A-CL";
                break;

            case 5: // '\005'
                prefix = "C-FR";
                break;

            case 6: // '\006'
                prefix = "F-FR";
                break;

            default:
                prefix = "";
                break;
        }
        String oldPrefix;
        switch (oldPieceType) {
            case 0: // '\0'
                oldPrefix = "D-CL";
                break;

            case 1: // '\001'
                oldPrefix = "C-CL";
                break;

            case 2: // '\002'
                oldPrefix = "BL-CL";
                break;

            case 3: // '\003'
                oldPrefix = "F-CL";
                break;

            case 4: // '\004'
                oldPrefix = "A-CL";
                break;

            case 5: // '\005'
                oldPrefix = "C-FR";
                break;

            case 6: // '\006'
                oldPrefix = "F-FR";
                break;

            default:
                oldPrefix = "";
                break;
        }
        java.util.Collection listPayment = crud.readAll(pieceId, oldPrefix, 26);
        if (listPayment != null) {
            HashSet ref = new HashSet(listPayment);
            Iterator it = ref.iterator();
            Payment payment = null;
            for (; it.hasNext(); crud.update(payment, 26)) {
                payment = (Payment) it.next();
                payment.setPrefix(prefix);
            }

        }
    }

    private void actionTransformeOrder() {
        boolean transforme = true;
        for (int i = 0; i < rowsData.length; i++) {
            if (rowsData[i][9].equals(red)) {
                transforme = false;
            }
        }

        if (transforme) {
            getReferencesPanel().setSelectedIndexPiece(2);
            int oldPieceType = pieceType;
            pieceType = getReferencesPanel().getSelectedIndexPiece();
            if (salesPiece != null) {
                salesPiece.setPrepare(new Integer(1));
                salesPiece.setPrefix("BL");
                actionUpdatePiece();
                actionUpdatePayment(oldPieceType);
            }
        }
    }

    private void actionTransformeReceptionOrder() {
        boolean transforme = true;
        for (int i = 0; i < rowsData.length; i++) {
            if (rowsData[i][9].equals(red)) {
                transforme = false;
            }
        }

        if (transforme) {
            getReferencesPanel().setSelectedIndexPiece(6);
            int oldPieceType = pieceType;
            pieceType = getReferencesPanel().getSelectedIndexPiece();
            if (buysPiece != null) {
                buysPiece.setPrefix("F");
                buysPiece.setDeliveryDate((new MEDate()).getPlain());
                actionUpdatePiece();
                actionUpdatePayment(oldPieceType);
            }
        }
    }

    public int getPieceType() {
        return pieceType;
    }

    public String getPieceId() {
        return pieceId;
    }

    public String getLastUpdatePiece() {
        if (salesPiece != null) {
            return salesPiece.getUpdateTime();
        }
        if (salesBillPiece != null) {
            return salesBillPiece.getUpdateTime();
        }
        if (buysPiece != null) {
            return buysPiece.getUpdateTime();
        } else {
            return null;
        }
    }

    public boolean isEditable() {
        return editable;
    }

    public boolean isLocked() {
        return lock;
    }

    public void setEditable(boolean editable) {
        if (!lock) {
            this.editable = editable;
            setReferencesPanelEnabled(editable);
            getRowsTable().setCellEditable(editable);
        }
    }

    public void setCustomer(String idCustomer) {
        if (editable) {
            if (buysPiece == null) {
                customer = (Customer) crud.read(idCustomer, 27);
                if (customer != null) {
                    initCustomerPanel();
                    actionUpdatePiece();
                }
            } else {
                JOptionPane.showMessageDialog(null, "Impossible d'enregistrer un client\nsur une pi\350ce de vente fournisseur !");
            }
        }
    }

    public void setSupplier(String idSupplier) {
        if (editable) {
            if (buysPiece != null) {
                supplier = (Supplier) crud.read(idSupplier, 9);
                if (supplier != null) {
                    initSupplierPanel();
                    actionUpdatePiece();
                }
            } else {
                JOptionPane.showMessageDialog(null, "Impossible d'enregistrer un fournisseur\nsur une pi\350ce de vente client !");
            }
        }
    }

    public void setItem(String idItem) {
        if (editable) {
            item = (Item) crud.read(idItem, 11);
            if (item != null) {
                BigDecimal quantity;
                BigDecimal rate;
                if (customer != null) {
                    String result = JOptionPane.showInputDialog(null, "Quantit\351");
                    try {
                        quantity = new BigDecimal(result);
                    } catch (NumberFormatException e) {
                        quantity = new BigDecimal(1);
                    }
                    rate = discount.getRate(customer, item, quantity);
                    setInfoField(discount.getMsg());
                } else {
                    quantity = new BigDecimal(1);
                    rate = new BigDecimal(0);
                }
                if (customer != null) {
                    BigDecimal pu = item.getPriceList1().subtract(item.getPriceList1().multiply(rate.movePointLeft(2))).setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue());
                    BigDecimal margin = pu.subtract(item.getPrixRevient()).divide(pu.movePointLeft(2), param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue()).setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue());
                    if (margin.compareTo(new BigDecimal(param.getMinimumProfitMargin())) == -1) {
                        JOptionPane.showMessageDialog(null, "<html><font color=red>ATTENTION VENTE A PERTE !\nL'article suivant : \nR\351f\351rence : " + item.getId() + "\nD\351signation : " + item.getDesignation() + "\nPrix de revient : " + item.getPrixRevient().setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue()) + "\nPrix de vente : " + item.getPriceList1().setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue()) + "\nEst vendu au prix de " + pu + " avec une marge de : " + margin + "%" + "\nalors que la marge minimale accept\351e est de : " + param.getMinimumProfitMargin() + "%");
                    }
                }
                int row;
                if (rowsList != null) {
                    row = rowsList.size();
                } else {
                    rowsList = new ArrayList();
                    row = 0;
                }
                updateRowsTable = false;
                getRowsTable().getTable().setValueAt(null, row, 0);
                getRowsTable().getTable().setValueAt(item.getId(), row, 1);
                getRowsTable().getTable().setValueAt(item.getDesignation(), row, 2);
                getRowsTable().getTable().setValueAt(new Integer(1), row, 3);
                getRowsTable().getTable().setValueAt(quantity, row, 4);
                if (customer != null) {
                    getRowsTable().getTable().setValueAt(item.getPriceList1().setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue()), row, 5);
                } else if (supplier != null) {
                    getRowsTable().getTable().setValueAt(item.getPriceList2().setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue()), row, 5);
                } else {
                    getRowsTable().getTable().setValueAt(item.getPriceList1().setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue()), row, 5);
                }
                getRowsTable().getTable().setValueAt(rate, row, 6);
                getRowsTable().getTable().setValueAt(null, row, 7);
                getRowsTable().getTable().setValueAt(null, row, 8);
                getRowsTable().getTable().setValueAt(item.getTvaId().toString(), row, 9);
                actionUpdateRow(row);
                updateRowsTable = true;
            }
        }
    }

    public OrderRow getSelectedOrderRow() {
        int row = getRowsTable().getTable().getSelectedRow();
        if (row == -1) {
            return null;
        } else {
            return (OrderRow) rowsList.get(row);
        }
    }

    public void updateOrderRow(ArrayList listOrderRow) {
        if (listOrderRow != null && listOrderRow.size() >= 1) {
            OrderRow or;
            for (Iterator it = listOrderRow.iterator(); it.hasNext(); actionUpdatePreparedRow(or)) {
                or = (OrderRow) it.next();
            }

            actionTransformeOrder();
        }
    }

    public void updateOrderRowReception(ArrayList listOrderRow) {
        if (listOrderRow != null && listOrderRow.size() >= 1) {
            OrderRow or;
            for (Iterator it = listOrderRow.iterator(); it.hasNext(); actionUpdateReceptionRow(or)) {
                or = (OrderRow) it.next();
            }

            actionTransformeReceptionOrder();
        }
    }

    public Agency getAgency() {
        return agency;
    }

    public void setAgency(Agency agency) {
        this.agency = agency;
    }

    public Customer getCustomer() {
        return customer;
    }

    public void setCustomer(Customer customer) {
        this.customer = customer;
    }

    public Supplier getSupplier() {
        return supplier;
    }

    public void setSupplier(Supplier supplier) {
        this.supplier = supplier;
    }

    public SalesPiece getSalesPiece() {
        return salesPiece;
    }

    public void setSalesPiece(SalesPiece salesPiece) {
        this.salesPiece = salesPiece;
    }

    public void updateSalesPiece() {
        if (salesPiece != null) {
            if (!crud.update(salesPiece, 22)) {
                JOptionPane.showMessageDialog(null, "Impossible d'enregistrer les donn\351es !\n(un autre utilisateur les a peut-\352tre modifi\351es entre temps)");
            } else {
                salesPiece = (SalesPiece) crud.read(salesPiece.getId(), 22);
            }
        }
    }

    public ArrayList getRowsList() {
        return rowsList;
    }

    public Object getAddress() {
        if (customer != null) {
            return customer;
        }
        if (supplier != null) {
            return supplier;
        } else {
            return null;
        }
    }

    public Object getPiece() {
        if (salesPiece != null) {
            return salesPiece;
        }
        if (salesBillPiece != null) {
            return salesBillPiece;
        }
        if (buysPiece != null) {
            return buysPiece;
        } else {
            return null;
        }
    }

    public SalesPerson getSalesPerson() {
        return salesPerson;
    }
    private JDesktopPane desktop;
    private ObjectCRUD crud;
    private Object taxData[][];
    private Object rowsData[][];
    private Agency agency;
    private SalesPerson salesPerson;
    private Customer customer;
    private Supplier supplier;
    private Item item;
    private OrderRow orderRow;
    private DiscountRate discount;
    private ModifyOrderController modifyOrderCtrl;
    private ShippingOrderController shippingOrderCtrl;
    private SalesPiece salesPiece;
    private SalesBillPiece salesBillPiece;
    private BuysPiece buysPiece;
    private int pieceType;
    private String pieceId;
    private String pieceDate;
    private ArrayList rowsList;
    private Icon green;
    private Icon orange;
    private Icon red;
    private JComboBox taxJComboBox;
    private String taxList[] = {
        "1", "2", "3", "4", "5"
    };
    private LabelCellRenderer labelRenderer;
    private TableColumnModel model;
    private Parameter param;
    private boolean calculateRow;
    private boolean editable;
    private boolean lock;
    private boolean updateRowsTable;
    private boolean write;
}
